﻿Public Class FrmTipoDoc

    Private result As GBDZafiro.Resultado

    Private Sub FrmTipoDoc_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        cargarGvTDoc()
    End Sub

    Protected Overrides Sub bnGuardar_Click(sender As Object, e As System.EventArgs)
        If Not Validaciones.camposVacios(pBase) Then
            If guardar() Then
                MyBase.bnGuardar_Click(sender, e)
                cargarGvTDoc()
            End If
        End If
    End Sub

    Protected Overrides Sub bnEditar_Click(sender As Object, e As System.EventArgs)
        If tbCodigo.TextLength < 1 Then
            msnBox.msnExclamacion("No ha seleccionado un documento!!!")

        Else
            MyBase.bnEditar_Click(sender, e)
            tbCodigo.Enabled = False

        End If
    End Sub

    Protected Overrides Sub bnEliminar_Click(sender As Object, e As System.EventArgs)
        If Not Validaciones.camposVacios(pBase) Then
            If msnBox.msnQuestion("¿Desea borrar el documento ?") = Windows.Forms.DialogResult.Yes Then
                result = GBDZafiro.TipoComprobante.Eliminar(CrearTComprobante())

                If result.ExisteError Then
                    msnBox.msnError(result.Mensaje)
                Else
                    MyBase.bnEliminar_Click(sender, e)
                    cargarGvTDoc()
                End If
            End If
        End If
    End Sub

    Private Sub gvTDoc_CurrentCellChanged(sender As System.Object, e As System.EventArgs)
        Dim row As DataGridViewRow = gvTDoc.CurrentRow
        If row IsNot Nothing Then
            tbCodigo.Text = row.Cells("COD_TIPO_CPB").Value
            tbDocumento.Text = row.Cells("DESCRIPCION").Value
        End If
    End Sub

    Private Sub cargarGvTDoc()
        RemoveHandler gvTDoc.CurrentCellChanged, AddressOf gvTDoc_CurrentCellChanged

        gvTDoc.DataSource = Nothing
        gvTDoc.DataSource = GBDZafiro.TipoComprobante.ConsultarAll()
        gvTDoc.Columns("FECHA_REGISTRO").Visible = False

        gvTDoc.Columns("COD_TIPO_CPB").HeaderText = "CODIGO"
        gvTDoc.Columns("DESCRIPCION").HeaderText = "DOCUMENTO"

        AddHandler gvTDoc.CurrentCellChanged, AddressOf gvTDoc_CurrentCellChanged
    End Sub

    Private Function guardar() As Boolean
        Dim tComp As GBDZafiro.TipoComprobante = CrearTComprobante()
        If GBDZafiro.TipoComprobante.Exist(tComp) Then
            result = GBDZafiro.TipoComprobante.Actualizar(tComp)
            If result.ExisteError Then
                msnBox.msnError(result.Mensaje)
            End If


        Else
            result = GBDZafiro.TipoComprobante.Nuevo(tComp)
            If result.ExisteError Then
                msnBox.msnError(result.Mensaje)
            End If


        End If
        Return Not result.ExisteError
    End Function

    Private Function CrearTComprobante() As GBDZafiro.TipoComprobante
        Dim tComp As New GBDZafiro.TipoComprobante
        tComp.COD_TIPO_CPB = tbCodigo.Text.ToUpper
        tComp.DESCRIPCION = tbDocumento.Text.ToUpper
        Return tComp
    End Function

End Class
